package so.ifl.manager.admin;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;

import so.ifl.commons.tools.Constant;
import so.ifl.commons.tools.MD5;
import so.ifl.servcie.AccountService;
import so.ifl.vo.AccountVo;

/**
 * 账号manager
 * create by simon
 * create at 2012-1-12 下午3:48:06   s 
 * <mailto:liangyuanliu@gmail.com>
 */
@Component("accountManager")
public class AccountManager {
	@Autowired
	private AccountService accountService;

	private Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	
	


	/**
	 * 账号分布查询
	 * simon
	 * create at 2012-1-12 下午12:04:22
	 * <mailto:liangyuanliu@gmail.com>
	 * @param page
	 * @param map
	 */
	@DataProvider
	public void getAccount(Page<AccountVo> page, Map<String, Object> map) {
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (map == null) {
			map = new HashMap<String, Object>();
		}
		List<AccountVo> list = accountService.queryAccountWithParamAndPagination(map, offset, limmit);
		int countsql = accountService.countAccountWithParam(map);
		page.setEntities(list);
		page.setEntityCount(countsql);
	}
	
	/**
	 * 保存更新过的用户拥有的角色信息
	 * create by aaron.lu
	 * create at 2012-1-18 上午12:46:04
	 * <mailto: atyuan.lu@gmail.com>
	 * @return
	 *
	 */
	@Expose
	public Map<String, Object> saveEditRole(String roleId, String accountId){
		Map<String, Object> result = new HashMap<String, Object>();
		String successFlag = Constant.SUCCESS_FALSE_FLAG;
		String msg = "";
		
		accountService.editRoleOfAccount(roleId, accountId);
		
		result.put("flag", successFlag);
		result.put("msg", msg);
		return result;
	}

	/**
	 * 账号信息cud
	 * simon
	 * create at 2012-1-12 下午12:07:26
	 * <mailto:liangyuanliu@gmail.com>
	 * @param offices
	 */
	@DataResolver
	public void updateAccount(Collection<AccountVo> accounts) {
		for (AccountVo account : accounts) {
			EntityState entityState = EntityUtils.getState(account);
			if (EntityState.NEW.equals(entityState)) {
				// 新增
				logger_.info("新增");
				insertAccount(account);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				// 修改
				logger_.info("修改");
				updateAccount(account);
			} else if (EntityState.DELETED.equals(entityState)) {
				// 删除
				logger_.info("删除");
				deleteAccount(account.getAccoutId());
			}
		}
	}

	/**
	 * 新增账号
	 * simon
	 * create at 2012-1-12 下午12:08:42
	 * <mailto:liangyuanliu@gmail.com>
	 * @param consult
	 */
	private void insertAccount(AccountVo account) {
		Map<String, Object> m = utilMap(account, true);
		accountService.insertAccount(m);
	}
	/**
	 * 删除账号
	 * simon
	 * create at 2012-1-13 下午5:34:36
	 * <mailto:liangyuanliu@gmail.com>
	 * @param accountId
	 */
	private void deleteAccount(Integer accountId)
	{
		accountService.deleteAccount(accountId);
	}
	
	/**
	 * 修改账号
	 * simon
	 * create at 2012-1-12 下午12:25:22
	 * <mailto:liangyuanliu@gmail.com>
	 * @param account
	 */
	private void updateAccount(AccountVo account)
	{
		Map<String, Object> m = utilMap(account, false);
		accountService.updateAccount(m);
	}

	/**
	 * 
	 * simon
	 * create at 2012-1-12 下午12:25:17
	 * <mailto:liangyuanliu@gmail.com>
	 * @param account
	 * @param add
	 * @return
	 */
	private Map<String, Object> utilMap(AccountVo account, boolean add) {
		Map<String, Object> m = new HashMap<String, Object>();
		if (add) {
			//donothing company从前台你选择
		}
		else
		{
			m.put("accout_id_", account.getAccoutId());
		}
		m.put("company_id", account.getCompanyId());
		m.put("account_login_name_", account.getAccountLoginName());
		m.put("account_pwd_", MD5.getMD5(account.getAccountPwd()));
		m.put("account_owner_", account.getAccountOwner());
		m.put("account_tel_", account.getAccountTel());
		m.put("account_email_", account.getAccountEmail());
		m.put("is_do_", account.getIsDo());
		m.put("account_note_", account.getAccountNote());
		
		return m;
	}

}
